/*
 * @Author: your name
 * @Date: 2021-07-09 10:07:09
 * @LastEditTime: 2021-07-22 11:09:33
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \cuiyuan_activity\assets\script\SceneStart.ts
 */
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html


import Game from "./Game";


const { ccclass, property } = cc._decorator;

@ccclass
export default class SceneStart extends cc.Component {

    scene_1: cc.Node = null;// 活动的第一个场景
    scene_2: cc.Node = null; // 活动的第二个场景
    scene_3: cc.Node = null;// 活动的第三个场景
    scene_4: cc.Node = null; // 活动的第四个场景


    static inst: SceneStart;


    onLoad() {
        SceneStart.inst = this;
        // 初始化组件
        this.scene_1 = cc.find('scene_1', this.node);
        this.scene_2 = cc.find('scene_2', this.node);
        this.scene_3 = cc.find('scene_3', this.node);
        this.scene_4 = cc.find('scene_4', this.node);
        // 初始化场景
        this.init();

    }

    // 初始化 场景
    init() {
        // 一些列的操作动画 使用函数回调减少代码的行数和增加可读性
        this.sceneAnimation(this.scene_1, () => {
            this.sceneAnimation(this.scene_2, () => {
                this.sceneAnimation(this.scene_3, () => {
                    this.sceneAnimation(this.scene_4, () => {
                        Game.inst.nextScene("ContentScene")
                        // 让第四个场景动画执行完成之后再隐藏节点
                        this.scheduleOnce(() => {
                            this.hide()
                        }, 1)
                    })
                })
            })
        })
    }

    /**
     * 对场景进行动画处理 连接处理
     * @param node 节点
     * @param callback 回调函数
     */
    sceneAnimation(node: cc.Node, callback: Function) {
        node.active = true;
        node.opacity = 0;
        cc.tween(node)
            .to(1, { opacity: 255 })
            .delay(4)
            .call(() => {
                // 将改场景隐藏，展示移动场景
                if (typeof callback === 'function') {
                    callback()
                }
            })
            .to(1, { opacity: 0 })
            .call(() => {
                node.active = false;
            })
            .start()
    }


    hide() {
        this.node.active = false;
    }
    show() {
        this.node.active = true;
    }

    start() {

    }

    // update (dt) {}
}
